<?php 

function searchEscorts($country_id,$state_id,$province_id,$city_id){
        $ci = &get_instance();
        $ci->db->set_dbprefix('default_');
        $escortCategoryId = getKeyOfArray( getSiteCategoryBaseOnSiteSetting() );
            
        $listingArr = array();
		if($state_id == -1){
			$listingArr[] = ($country_id != 0) ? "country_id=$country_id" :'1';
			$listingCond = " (".implode(' AND ',$listingArr).") ";
		}else{
			$listingArr[] = ($country_id != 0) ? "country_id=$country_id" :'1';
			$listingArr[] = ($state_id != 0) ? "state_id=$state_id" :'1';
			$listingArr[] = ($province_id != 0) ? "province_id=$province_id" :'1';
			$listingArr[] = ($city_id != 0) ? "city_id=$city_id" :'1';
			$listingCond = " (".implode(' AND ',$listingArr).") ";
		}
		$rs = $ci->db->query("SELECT * FROM ".TBL_PLACE_LIST." WHERE $listingCond")->result();
        
        if($rs){
			$tmpArr = array();
			foreach($rs as $item){
				$regexp = "[[:<:]]{$item->id}[[:>:]]";
				$tmpArr[] = " B.places_list_ids_contract REGEXP '$regexp' ";
			}
			$listingPlaceCond = " (".implode(" OR ",$tmpArr).") ";
		}else{
			$listingPlaceCond = "1=0";
		}
        
        $condition = "$listingPlaceCond AND B.escort_category_id=$escortCategoryId ";
		$query = "SELECT DISTINCT B.id AS escort_id FROM ".TBL_USER. //
						" AS A INNER JOIN ".TBL_ESCORT." AS B ON A.id=B.user_id WHERE $condition ORDER BY order_value DESC";
						
		$allResult = $ci->db->query($query)->result();
		return $allResult;
    }